<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\dao;


use ckeeper\db\Model;
use ckeeper\entity\ManagerEntity;
use Selibra\Model\Exception\DbException;
use Selibra\ORM\Annotations\Entity;
use Selibra\ORM\Annotations\Table;

#[Table("manager")]
#[Entity(ManagerEntity::class)]
class ManagerDao extends Model
{

    /**
     * @param string $username
     * @return ManagerEntity
     */
    public function getManagerAccountByUsername(string $username): ManagerEntity
    {
        $this->getCondition()
            ->andWhere('delete_time', '=', 0)
            ->andWhere('username', '=', $username);
        return $this->get();
    }

    /**
     * @param string $token
     * @return ManagerEntity
     */
    public function getManagerAccountByToken(string $token): ManagerEntity
    {
        $this->getCondition()
            ->andWhere('delete_time', '=', 0)
            ->andWhere('token', '=', $token);
        return $this->get();
    }


    /**
     * @param string $uuid
     * @param string $token
     * @throws DbException
     */
    public function updateLoginToken(string $uuid, string $token)
    {
        $this->getCondition()->andWhere('uuid', '=', $uuid);
        $this->update([
            'token' => $token,
            'update_time' => time()
        ]);
    }

}
